/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ace.states;

import ace.Main;
import ace.component.ButtonPush;
import ace.component.ButtonWheel;
import ace.game.entity.player.Profile;
import ace.system.BitmapFont;
import ace.system.Model;
import ace.system.StateManager;
import ace.system.Utilities;
import ace.system.sound.SFXPlayer;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author TimotiusNC
 */
public class StateProfile extends GameState implements CommandListener{
	public static final int MAX_PROFILE = 3;

	public StateProfile(){
		super(GameState.PROFILE, StateManager.instance().getBackgroundImage());

		initComp();
	}

	private void initComp(){
		m_OffsetX = 5;
		m_OffsetY = 5;
		m_ProfileCount = 0;

		m_BtnWheel = new ButtonWheel(3, 1, "/profile_panel_selected.png", null);
		m_BtnWheel.addButton(0, new ButtonPush(0, Utilities.INTERFACE_FOLDER + "/profile_panel.png", m_OffsetX, m_OffsetY));
		m_BtnWheel.addButton(1, new ButtonPush(1, Utilities.INTERFACE_FOLDER + "/profile_panel.png", m_OffsetX+60, m_OffsetY+85));
		m_BtnWheel.addButton(2, new ButtonPush(2, Utilities.INTERFACE_FOLDER + "/profile_panel.png", m_OffsetX+95, m_OffsetY+170));
		m_BtnWheel.setSelection1Offset(0, 0);
		m_BtnWheel.setImmediate(false);
		m_BtnWheel.setSelectedID(0);
		
        m_NoProfileImage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Empty Profile", 0x000000);

		m_OKCommand	= new Command("OK", Command.OK, 1);
		m_TextBox = new TextBox("Insert your name", "", 10, TextField.INITIAL_CAPS_SENTENCE);
		m_TextBox.addCommand(m_OKCommand);
		m_TextBox.setCommandListener(this);

		initProfile();
	}

	private void initProfile(){
		m_ProfileCount = Utilities.getInt(1, -1);

		m_Profiles		= new Profile[MAX_PROFILE];
		m_ImageNames	= new Image[MAX_PROFILE];
		m_ImageLevels	= new Image[MAX_PROFILE];

		for(int i=0; i<m_ProfileCount; ++i){
			m_Profiles[i]		= new Profile(Model.getModelByteData(i));
			m_ImageNames[i]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(m_Profiles[i].getProfileName(), 0x000000);
			m_ImageLevels[i]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Level " + m_Profiles[i].getLevel(), 0x000000);
		}
	}

	public void keyPressed(int key) {
		super.keyPressed(key);

		//Is right pressed?
		if (key == -7 || key == 35) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CANCEL);

			//Exit
			m_Active = false;
		}
	}

	public void update(long time, int keys) {
		super.update(time, keys);

		if( ((keys & GameCanvas.FIRE_PRESSED) != 0) ){
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_SELECT);
			int selected = m_BtnWheel.getSelectedButton().getID();
			Vector param = new Vector();

			if(selected < m_ProfileCount){ //Existing Profile, Goto menu
				param.addElement(m_Profiles[selected]);
				StateManager.instance().goTo(GameState.MENU, param, false);
			} else { //Create new profile
				Display.getDisplay(Main.Midlet).setCurrent(m_TextBox);
			}
		}

		m_BtnWheel.forceDraw();
		m_BtnWheel.update(keys);

		if(m_BtnWheel.getSelectedButton().getID() > m_ProfileCount)
			m_BtnWheel.setSelectedID(m_ProfileCount == 0 ? 0 : m_ProfileCount-1);
		else if( ((keys & GameCanvas.UP_PRESSED) != 0) || ((keys & GameCanvas.DOWN_PRESSED) != 0) ){
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CURSOR);
		}
	}

	public void draw(Graphics g) {
		super.draw(g);

		//Dark gradient
		g.drawImage(StateManager.instance().getDarkImage(), 0, 0, Graphics.LEFT | Graphics.TOP);

		//Draw Profile Panel Button Wheel
		m_BtnWheel.draw(g);

		//Draw Profile
		for(int i=0; i<StateProfile.MAX_PROFILE; ++i){
			switch(i){
				case 0:
					if(m_ProfileCount < 1)
						g.drawImage(m_NoProfileImage, m_OffsetX+65, m_OffsetY+5+20, Graphics.LEFT | Graphics.TOP);
					else{
						g.drawImage(m_ImageNames[i], m_OffsetX+65, m_OffsetY+20, Graphics.LEFT | Graphics.TOP);
						g.drawImage(m_ImageLevels[i], m_OffsetX+75, m_OffsetY+32, Graphics.LEFT | Graphics.TOP);
					}
					break;
				case 1:
					if(m_ProfileCount < 2)
						g.drawImage(m_NoProfileImage, m_OffsetX+60+65, m_OffsetY+90+20, Graphics.LEFT | Graphics.TOP);
					else{
						g.drawImage(m_ImageNames[i], m_OffsetX+60+65, m_OffsetY+85+20, Graphics.LEFT | Graphics.TOP);
						g.drawImage(m_ImageLevels[i], m_OffsetX+60+75, m_OffsetY+85+32, Graphics.LEFT | Graphics.TOP);
					}
					break;
				case 2:
					if(m_ProfileCount < 3)
						g.drawImage(m_NoProfileImage, m_OffsetX+95+65, m_OffsetY+175+20, Graphics.LEFT | Graphics.TOP);
					else{
						g.drawImage(m_ImageNames[i], m_OffsetX+95+65, m_OffsetY+170+20, Graphics.LEFT | Graphics.TOP);
						g.drawImage(m_ImageLevels[i], m_OffsetX+95+75, m_OffsetY+170+32, Graphics.LEFT | Graphics.TOP);
					}
					break;
			}
		}
	}

	//Button
	protected ButtonWheel	m_BtnWheel;
	protected TextBox		m_TextBox;
	protected Command		m_OKCommand;

	//Data
	protected int		m_ProfileCount;
	protected Profile[]	m_Profiles;

	//Images
	protected Image m_NoProfileImage;
	protected Image[] m_ImageNames, m_ImageLevels;

	//Offset
	protected int	m_OffsetX, m_OffsetY;

	public void commandAction(Command c, Displayable d) {
		if(c == m_OKCommand){
			Display.getDisplay(Main.Midlet).setCurrent(StateManager.instance());

			if(!m_TextBox.getString().equals("")){
				int profileIdx = m_BtnWheel.getSelectedButton().getID();
				
				m_ImageNames[profileIdx]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(m_TextBox.getString(), 0x00000000);
				m_ImageLevels[profileIdx]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Level 1", 0x00000000);

				m_Profiles[profileIdx] = new Profile(m_TextBox.getString(), profileIdx);
				Model.writeModel(profileIdx, m_Profiles[profileIdx]);
				
				m_ProfileCount++;
				if(m_ProfileCount > MAX_PROFILE)
					m_ProfileCount = MAX_PROFILE;

				Utilities.putInt(1, m_ProfileCount);	//Save profile count to RMS

				//Go to StateMenu
				Vector param = new Vector();
				param.addElement(m_Profiles[profileIdx]);
				StateManager.instance().goTo(GameState.MENU, param, false);
			}
		}
	}
}
